package com.tarry.nio;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

public class FileInputChannel {

    public static void main(String[] args) {
        try (FileInputStream fileInputStream = new FileInputStream("./nio_test.txt")) {
            final FileChannel channel = fileInputStream.getChannel();
            // 根据文件实际长度读取内容
            final ByteBuffer byteBuffer = ByteBuffer.allocate(fileInputStream.available());
            channel.read(byteBuffer);
            System.out.println(new String(byteBuffer.array(), StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
